#ifndef __MC_PARAM_H
#define __MC_PARAM_H
#include "main.h"
#include "stdint-gcc.h"

#define SECTOR_1    1u
#define SECTOR_2    2u
#define SECTOR_3    3u
#define SECTOR_4    4u
#define SECTOR_5    5u
#define SECTOR_6    6u

#define IA_CURR_CH  6U
#define IB_CURR_CH  7U
#define IC_CURR_CH  14U
#define ADC_INJ_SHT 15U

#define TNOISE_NS                     2550U
#define TRISE_NS                      2550U
#define MAX_TNTR_NS TRISE_NS

#define DEADTIME_NS         800U
#define ADV_TIM_CLK_MHz     168U
#define ADC_CLK_MHz         21U
#define ADC_SAMPLING_CYCLES 3U
#define SAMPLING_TIME ((ADC_SAMPLING_CYCLES * ADV_TIM_CLK_MHz) / ADC_CLK_MHz)
#define TW_AFTER ((uint16_t)(((DEADTIME_NS+MAX_TNTR_NS)*ADV_TIM_CLK_MHz)/1000ul))
#define MAX_TWAIT ((uint16_t)((TW_AFTER - SAMPLING_TIME)/2))

#define MAX_DUTY 3600U
#define ADC_DEAD_TIME 100U

#define MOTOR_POLESPAIR 7

#define USE_STM32F405 0U
#define USE_STM32F103 1U

#define KI_D   13106      //1~32767
#define KP_D   1310
#define KI_Q   1310
#define KP_Q   1310

typedef enum
{
    control_null,
    torque_mode,
    speed_mode,
    
    postion_mode,
    tor_pos_mod,

}ControlMethod;

typedef struct
{
    int16_t d;
    int16_t q;
}MC_Dq_t;

typedef struct
{
    int16_t alpha;
    int16_t beta;
}MC_AlphaBeta_t;

typedef struct 
{
  int32_t u;//unti clark out u
  int32_t v;//unti clark out v
  int32_t w;//unti clark out w

  int32_t ta;
  int32_t tb;
  int32_t tc;
}MC_Uvw_t;

typedef struct
{

    TIM_TypeDef* foc_timmer;
    int16_t ia;
    int16_t ib;
    int16_t ic;
    MC_Dq_t idq;
    MC_Dq_t udq;
    MC_Dq_t idq_ref;
    MC_AlphaBeta_t ialpha_beta;
    MC_AlphaBeta_t ualpha_beta;
    MC_Uvw_t mc_uvw;
    int16_t theta;  //electric angle 
    uint16_t sa;    //timer duty output of h side a
    uint16_t sb;    //timer duty output of h side b
    uint16_t sc;    //timer duty output of h side c
    uint16_t sa_act;    //timer duty output of h side a
    uint16_t sb_act;    //timer duty output of h side b
    uint16_t sc_act;    //timer duty output of h side c
    uint16_t low_duty;  //Longest open time of h side
    uint16_t hight_duty;//shortest open time of h side
    uint16_t mid_duty;  //mid open time of h side
    uint16_t half_duty; //constant value of pwm mid duty, mornam is 5250/2 = 2625
    uint16_t pwm_duty_adc_time;//for adc sample time 2*Ts + Tc, where Ts = Sampling time of ADC, Tc = Conversion Time of ADC 
    uint16_t ch4_cnt;
    ControlMethod control_mod;

    int16_t ia_norm;//adc 1.65v ~ 32767  0V~-32767
    int16_t ib_norm;//adc 1.65v ~ 32767  0V~-32767
    int16_t ic_norm;//adc 1.65v ~ 32767  0V~-32767

    int32_t spd_ref;//

}McFocParam_t;

typedef struct
{
    ADC_TypeDef *adc1;  //ADC1
    ADC_TypeDef *adc2;  //ADC2
    uint32_t adc_trigger_edge;  //1：RISING EDGE OR 0： FALLING EDGE
    uint32_t adc1_inject_table[6];   //ADC INJECT CHANNEL RECONFIG CORROSPAND TO ROTARY SECTION
    uint32_t adc2_inject_table[6];   //ADC INJECT CHANNEL RECONFIG CORROSPAND TO ROTARY SECTION
    uint32_t adc1_value;            //ADC CONVERT VALUE OF INJECT CHANNEL 1
    uint32_t adc2_value;            //ADC CONVERT VALUE OF INJECT CHANNEL 2
    uint32_t adc3_value;            //ADC REGULATOR CONVERT VALUE          
    uint32_t phase_a_offset;
    uint32_t phase_b_offset;
    uint32_t phase_c_offset;
}ADCParam_t;




typedef struct
{
    uint16_t raw;//for spi encoder
    uint32_t encoder_count_now;
    int32_t encoder_count_pre[5];
    int16_t m_angle;    //machnical angle after minus raw data at '0' degree
    int32_t e_angle;    //electric angle corrosponder to machnical angle 
    int32_t e_angle_act;
    uint8_t sector;
    int32_t encoder_offset;
    int32_t virtual_theta;
    int32_t act_theta;
    uint8_t sector_act;
    int32_t speed_rad;//spd rad/s
    int32_t speed_arr[10];
    int32_t speed_rad_nom;//25rad/s ~ 32767, used as pid input
}EncoderData_t;

typedef struct
{   
    uint16_t sw_error;
}MCError_t;

typedef struct
{
    ADCParam_t      adc_content;        //adc value
    McFocParam_t    mc_foc_param;       //foc convert needed content
    MCError_t       mc_error;
    EncoderData_t   encoder_content;    //encoder's extent value
}Motor_t;






#endif

